/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Drawing;

using SdlDotNet.Graphics;
using Sdl = SdlDotNet.Graphics;
using SdlDotNet.Core;
using SdlDotNet.Input;

using BlindShark.GameElements;
using BlindShark.Widgets;

using Gulix.Audio;

namespace BlindShark.GameStates
{
	public enum TrackResultType
	{
		Dismiss,
		GoodAnswer,
		BadAnswer,
		TrackEnd,
		TimeLimit
	}
	
	public class GS_TrackResult : GameState
	{
		// Surfaces
		Surface _sfFrame;
		
		// Elements location
		Rectangle _rctFrame;
		
		// Labels
		Label _lblTitle;
		Label _lblTrackInfo;
		Label _lblTrackOwner;
		Label _lblMessage;
		Label _lblPressKey;
		Label _lblScore;
		
		// Events
		EventHandler<KeyboardEventArgs> _ehKeyPressed;
		
		// Texts
		string _sTitle;
		string _sMessage;
		string _sScore;
		
		//string sFont;		
		
		TrackResultType _trtScreenType;
		
		TimeEvent _tmeWait;
		bool _bScreenActive;
		
		public GS_TrackResult(TrackResultType trackResultType) : this(trackResultType, 0)
		{
			
		}
		
		public GS_TrackResult(TrackResultType trackResultType, int scoreEvolution)
		{
			_trtScreenType = trackResultType;
			
			InitStrings(scoreEvolution);
			
			// Initializing the labels
			// Title
			_lblTitle = new Label(_sTitle, 36, Color.OrangeRed, TextAlignment.TopMiddle);
			_lblTitle.Underline = true;
			_lblTitle.Location = new Point(0,15);
			// Track Information
			_lblTrackInfo = new Label(GameManager.CurrentTrack.Description, 20, Color.Red, TextAlignment.TopMiddle);
			_lblTrackInfo.Location = new Point(0, 120);
			// Track Owner
			if (GameManager.ChallengeMode)
			{
				_lblTrackOwner = new Label(LanguageManager.Get("TR_By") + PlayerManager.GetName(GameManager.CurrentTrackOwner),
			                           20, Color.Red, TextAlignment.TopMiddle);
				_lblTrackOwner.Location = new Point(0, 150);
			}
			// Message
			_lblMessage = new Label(_sMessage, 30, Color.Orange, TextAlignment.TopMiddle);
			_lblMessage.Location = new Point(0, 200);
			// "Press Any Key" message
			_lblPressKey = new Label(LanguageManager.Get("TR_Press"), 24, Color.AntiqueWhite, TextAlignment.TopMiddle);
			_lblPressKey.Location = new Point(0,450);
			// Score evolution
			_lblScore = new Label(_sScore, 28, Color.Orange, TextAlignment.TopMiddle);
			_lblScore.Location = new Point(0,250);
			
			// Keyboard events
			Events.Remove();
			_ehKeyPressed = new EventHandler<KeyboardEventArgs>(KeyPressed);
			Events.KeyboardUp += _ehKeyPressed;
		}
		
		public void Draw()
		{
			ScreenManager.MainScreen.Blit(_sfFrame, _rctFrame.Location);			
		}
		
		
		public void CleanUp()
		{
			Events.KeyboardUp -= _ehKeyPressed;
			Events.Remove();
		}
		
		public void HandleEvents()
		{
			Events.Poll();
		}
		
		public void Update()
		{
			AudioEngine.Update();
		}
		
		public void Init()
		{
			// Initializing the frame
			_sfFrame = new Surface(SurfaceManager.GetSurface("gfx/background_infoWindow.png").Size);
			_sfFrame.Blit(SurfaceManager.GetSurface("gfx/background_infoWindow.png"));
			_rctFrame = new Rectangle(new Point(150,50), _sfFrame.Size);
			
			// Initializing the title
			_lblTitle.Width = _rctFrame.Width;
			_lblTitle.AutoHeight = true;
			_lblTitle.Init();
			_sfFrame.Blit(_lblTitle.Surface, _lblTitle.Location);
			
			// Initializing the Track Information
			_lblTrackInfo.Width = _rctFrame.Width;
			_lblTrackInfo.AutoHeight = true;
			_lblTrackInfo.Init();
			
			// Displaying the track's description and the track's owner (if needed
			if ((_trtScreenType!=TrackResultType.BadAnswer) || (PlayerManager.CountPlaying==0))
			{
				_sfFrame.Blit(_lblTrackInfo.Surface, _lblTrackInfo.Location);
				// In challenge mode, displaying the owner
				if (GameManager.ChallengeMode)
				{
					// Initializing the Track Owner
					_lblTrackOwner.Width = _rctFrame.Width;
					_lblTrackOwner.AutoHeight = true;
					_lblTrackOwner.Init();
					_sfFrame.Blit(_lblTrackOwner.Surface, _lblTrackOwner.Location);
				}
			}
			
			// Initializing the message
			_lblMessage.Width = _rctFrame.Width;
			_lblMessage.AutoHeight = true;
			_lblMessage.Init();
			_sfFrame.Blit(_lblMessage.Surface, _lblMessage.Location);
			
			// Initializing the "Press Any Key" message
			_lblPressKey.Width = _rctFrame.Width;
			_lblPressKey.AutoHeight = true;
			_lblPressKey.Init();
			if (_bScreenActive)
				_sfFrame.Blit(_lblPressKey.Surface, _lblPressKey.Location);
			
			// Initializing the Score
			_lblScore.Width = _rctFrame.Width;
			_lblScore.AutoHeight = true;
			_lblScore.Init();
			_sfFrame.Blit(_lblScore.Surface, _lblScore.Location);
			
			// Replaying the track
			if ((AudioEngine.MusicPaused) && (_trtScreenType!=TrackResultType.TrackEnd) &&
			    ((_trtScreenType!=TrackResultType.BadAnswer) || (PlayerManager.CountPlaying==0)))
			{
				AudioEngine.PauseMusic();
				AudioEngine.EndMusic += new EndMusicEventHandler(EndMusic);
			} else if (_trtScreenType==TrackResultType.TimeLimit) {
				AudioEngine.EndMusic += new EndMusicEventHandler(EndMusic);
			}
			
			// Wait before active screen
			_tmeWait = new TimeEvent(1, 2000.0f, new TimeEvent.Call(WaitFinished));
			ClockManager.AddTimeEvent(_tmeWait);
			_bScreenActive = false;
		}
		
		public void KeyPressed(object sender, EventArgs e)
		{
			if (!_bScreenActive)
				return;
			
			if ((_trtScreenType!=TrackResultType.BadAnswer) || (PlayerManager.CountPlaying==0))
				GameStateManager.ChangeState(new GS_ChangeTrack(false));
			else
				GameStateManager.ChangeState(new GS_CheckPlayers());
		}
		
		private void EndMusic(object sender, EventArgs e)
		{
			GameStateManager.ChangeState(new GS_TrackResult(TrackResultType.TrackEnd));
		}
		
		public void WaitFinished()
		{
			_bScreenActive = true;
			_sfFrame.Blit(_lblPressKey.Surface, _lblPressKey.Location);
		}
		
		private void InitStrings(int iScore)
		{
			_sTitle = "";
			_sMessage = "";
			_sScore = "";
			// Setting the texts depending on the Screen Type
			
			switch(_trtScreenType)
			{
				case TrackResultType.Dismiss:
					_sTitle = LanguageManager.Get("TR_GiveUp");
					_sMessage = LanguageManager.Get("TR_GiveUpMessage");
					break;
				case TrackResultType.GoodAnswer:
					_sTitle = LanguageManager.Get("TR_Good");;
					_sMessage = LanguageManager.Get("TR_GoodMessage");;
					if (iScore>0)
					{
						_sScore = "+ "+iScore.ToString()+" point";
						_sScore += (iScore>1) ? "s" : "";
					}
					break;
				case TrackResultType.BadAnswer:
					_sTitle = LanguageManager.Get("TR_Bad");;
					_sMessage = LanguageManager.Get("TR_BadMessage");;
					if (iScore>0)
					{
						_sScore = "- "+iScore.ToString()+" "+
							((iScore>1) ? LanguageManager.Get("TR_MorePoints") : LanguageManager.Get("TR_OnePoint"));
					}
					break;
				case TrackResultType.TrackEnd:
					_sTitle = LanguageManager.Get("TR_End");;
					_sMessage = LanguageManager.Get("TR_EndMessage");;
					break;
				case TrackResultType.TimeLimit:
					_sTitle = LanguageManager.Get("TR_Limit");;
					_sMessage = LanguageManager.Get("TR_LimitMessage");;
					break;
			}
		}
	}
}
